package com.choichumgming.rentcat.filter;



import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Map;

/**
 * The class Auth header filter.
 *
 * @author paascloud.net @gmail.com
 */
@Component
public class AuthHeaderFilter extends ZuulFilter {
    private static final Logger LOGGER = LoggerFactory.getLogger(AuthHeaderFilter.class);

    private static final String BEARER_TOKEN_TYPE = "Bearer ";
    private static final String OPTIONS = "OPTIONS";
    private static final String UMS = "/ums";
    private static final String PMS = "/pms";
    private static final String MMS = "/mms";
    private static final String OMS = "/oms";
    private static final String AMS = "/ams";

    /**
     * Filter type string.
     *
     * @return the string
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
     * Filter order int.
     *
     * @return the int
     */
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * Should filter boolean.
     *
     * @return the boolean
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * Run object.
     *
     * @return the object
     */
    @Override
    public Object run() {
        RequestContext requestContext = RequestContext.getCurrentContext();
        try {
            doSomething(requestContext);
        } catch (Exception e) {
            throw new RuntimeException();
        }
        return null;
    }

    private void doSomething(RequestContext requestContext) throws ZuulException {
        HttpServletRequest request = requestContext.getRequest();
        String requestURI = request.getRequestURI();

        if (OPTIONS.equalsIgnoreCase(request.getMethod()) || !requestURI.contains(UMS) || !requestURI.contains(PMS) || !requestURI.contains(AMS)) {
            LOGGER.warn(requestURI);
            return;
        }
        String authHeader =  request.getHeader("token");
        LOGGER.warn(authHeader);
        if (isEmpty(authHeader)) {
            LOGGER.warn(authHeader);
            return;
        }

        if (authHeader.startsWith(BEARER_TOKEN_TYPE)) {
            requestContext.addZuulRequestHeader(HttpHeaders.AUTHORIZATION, authHeader);
        }
    }
    public static boolean isEmpty(Object pObj) {
        if (pObj == null) {
            return true;
        }
        if (pObj == "") {
            return true;
        }
        if (pObj instanceof String) {
            return ((String) pObj).length() == 0;
        } else if (pObj instanceof Collection) {
            return ((Collection) pObj).isEmpty();
        } else if (pObj instanceof Map) {
            return ((Map) pObj).size() == 0;
        }
        return false;
    }


}
